<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div>
          <el-input v-model="searchTerms.searchValue" placeholder="请输入表单名称" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
        <div>
          <router-link to="./create">
            <el-button type="primary">创建申请</el-button>
          </router-link>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="100" header-align="center" align="center" sortable />
        <el-table-column prop="name" label="申请名称" min-width="120"/>
        <el-table-column prop="createTime" label="创建时间" width="180" header-align="center" align="center" sortable/>
        <el-table-column prop="status" label="状态" width="160" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===1" type="success">已开启</el-tag>
            <el-tag v-else type="info">已关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="260" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <router-link :to="`./detailList?id=${scope.row.id}`">
              <el-button type="primary" size="small">详情</el-button>
            </router-link>
            <router-link :to="`./modify?id=${scope.row.id}`">
              <el-button type="success" class="bg-green" size="small">编辑</el-button>
            </router-link>
            <el-button type="danger" size="small" @click="deleteForm(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
import { handleCurrentChange, handleSizeChange, handleSortChange } from '@/utils/table'

export default {
  data() {
    return {
      searchTerms: {
        searchValue: '',
        type: ''
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,

      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  watch: {

  },
  created() {
    this.getData()
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'apply/getPageList'
      const data = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        search: this.searchTerms.searchValue
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    deleteForm(row) {
      this.$confirm('此操作将删除该表单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'apply/delete';
        const data = {
          id: row.id
        }
        this.$store.dispatch('GetConnect', { url, data }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error(e.msg + ',请刷新或联系管理员')
        })
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  max-width: 1400px;
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
    }
    .el-select{
      width: 140px;
      margin-right: 10px;
    }
    .search-input {
      width: 240px;
    }
    .do-btn {
      @extend .bg-green;
      margin-left: 6px;
    }
  }
}
</style>
